#pragma once
#include <string>
#include "data/sql/sql_expr.hpp"
#include "data/sql/expr_node.hpp"
#include <format>

namespace blitz_query_cpp::sql
{

    class postgresql_renderer
    {
        std::vector<expr_node> node_stack;
        std::string buffer;
        std::string error_msg;

        bool handle_binary_op(expr_node &current, std::string_view op);
        bool handle_join(expr_node &current, std::string_view join_type);
        void visit_children(expr_node &node, std::string_view separator = {}, std::string_view end = {});
        void write_quoted_value(expr_node &node);
        void push_value(std::string_view value);
        void push_node(const sql_expr_t &node);

        template <class... Args>
        bool report_error(std::string_view fmt, Args &&...args)
        {
            error_msg = std::vformat(fmt, std::make_format_args(args...));
            return false;
        }

    public:
        postgresql_renderer();
        bool render(const sql::sql_expr_t &expr);
        const std::string &get_string() { return buffer; }
        const std::string &get_error_msg() { return error_msg; }

    };
}